<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/27
 * Time: 15:40
 */

namespace common\sources\write\businessacount;

use Yii;
use common\models\craftsman\BusinessAccount;

class BusinessAcountAccess implements IBusinessAcountAccess
{
    public function modifyBusAccountCash($data)
    {
        $sql = 'UPDATE ' . BusinessAccount::tableName() . ' SET total_deposit = :total_deposit,ava_deposit = :ava_deposit,freezing_deposit = :freezing_deposit,ava_pepper = :ava_pepper,
        freezing_golds = :freezing_golds,cash_deposit = :cash_deposit,
        update_time = :update_time WHERE bus_id = :bus_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':total_deposit' => $data['total_deposit'],
                ':ava_deposit' => $data['ava_deposit'],
                ':freezing_deposit' => $data['freezing_deposit'],
                ':ava_pepper' => $data['ava_pepper'],
                ':freezing_golds' => $data['freezing_golds'],
                ':cash_deposit' => $data['cash_deposit'],
                ':update_time' => time(),
                ':bus_id' => $data['bus_id'],
            ])->execute();
    }
    public function modifyBusinessAccount($data)
    {
        $sql = 'UPDATE ' . BusinessAccount::tableName() . ' SET total_deposit = :total_deposit,ava_deposit = :ava_deposit,freezing_deposit = :freezing_deposit,freezing_golds = :freezing_golds,update_time = :update_time WHERE bus_id = :bus_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':total_deposit' => $data['total_deposit'],
                ':ava_deposit' => $data['ava_deposit'],
                ':freezing_deposit' => $data['freezing_deposit'],
                ':freezing_golds' => $data['freezing_golds'],
                ':update_time' => time(),
                ':bus_id' => $data['bus_id'],
            ])->execute();
    }
    public function modifyBusinessPepper($data)
    {
        $sql = 'UPDATE ' . BusinessAccount::tableName() . ' SET ava_pepper = :ava_pepper,update_time = :update_time WHERE bus_id = :bus_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':ava_pepper' => $data['ava_pepper'],
                ':update_time' => time(),
                ':bus_id' => $data['bus_id'],
            ])->execute();
    }

    public function modifyBusPepper($data)
    {
        $sql = 'UPDATE ' . BusinessAccount::tableName() . ' SET total_deposit = :total_deposit,ava_deposit = :ava_deposit,ava_pepper = :ava_pepper,freezing_golds = :freezing_golds,update_time = :update_time WHERE bus_id = :bus_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':total_deposit' => $data['total_deposit'],
                ':ava_deposit' => $data['ava_deposit'],
                ':ava_pepper' => $data['ava_pepper'],
                ':freezing_golds' => $data['freezing_golds'],
                ':update_time' => time(),
                ':bus_id' => $data['bus_id']
            ])->execute();

    }

    public function modifyAcount($data)
    {
        $sql = 'UPDATE ' . BusinessAccount::tableName() . ' SET total_deposit = :total_deposit,ava_deposit = :ava_deposit,ava_pepper = :ava_pepper,freezing_golds = :freezing_golds,freezing_deposit = :freezing_deposit,update_time = :update_time WHERE bus_id = :bus_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':total_deposit' => $data['total_deposit'],
                ':ava_deposit' => $data['ava_deposit'],
                ':ava_pepper' => $data['ava_pepper'],
                ':freezing_golds' => $data['freezing_golds'],
                ':freezing_deposit' => $data['freezing_deposit'],
                ':update_time' => time(),
                ':bus_id' => $data['bus_id']
            ])->execute();
    }

    public function changeBranchBank($data) {
        $userAmount = BusinessAccount::find()
            ->where(['bus_id' => $data['id']])
            ->one();
        if ($userAmount) {
            if (1 == $data['change']) {//调整押金
                switch ($data['action_type']) {
                    case 1://充值
                        $userAmount->total_deposit = number_format($userAmount->total_deposit + $data['money'],2,".","");
                        $userAmount->ava_deposit = number_format($userAmount->ava_deposit + $data['money'],2,".","");
                        break;
                    case 2://扣减
                        $userAmount->total_deposit = number_format($userAmount->total_deposit - $data['money'],2,".","");
                        $userAmount->ava_deposit = number_format($userAmount->ava_deposit - $data['money'],2,".","");
                        break;
                    case 3://冻结
                        $userAmount->ava_deposit = number_format($userAmount->ava_deposit - $data['money'],2,".","");
                        break;
                    case 4://解冻
                        $userAmount->ava_deposit = number_format($userAmount->ava_deposit + $data['money'],2,".","");
                        break;
                    case 5://扣除解冻
                        $userAmount->total_deposit = number_format($userAmount->total_deposit - $data['money'],2,".","");
                        break;
                    default:
                        break;
                }
            }
            $userAmount->update_time = time();
            if ($userAmount->save()) return $userAmount;
        }
        return false;
    }

    public function changeBranchPepper($data) {
        $userAmount = BusinessAccount::find()
            ->where(['bus_id' => $data['id']])
            ->one();
        if ($userAmount) {
            if (2 == $data['change']) {//调整花椒
                switch ($data['action_type']) {
                    case 1://充值
                        $userAmount->ava_pepper = number_format($userAmount->ava_pepper + $data['money'],2,".","");
                        break;
                    case 2://扣减
                        $userAmount->ava_pepper = number_format($userAmount->ava_pepper - $data['money'],2,".","");
                        break;
                    case 3://冻结
                        $userAmount->ava_pepper = number_format($userAmount->ava_pepper - $data['money'],2,".","");
                        $userAmount->freezing_golds = number_format($userAmount->freezing_golds + $data['money'],2,".","");
                        break;
                    case 4://解冻
                        $userAmount->freezing_golds = number_format($userAmount->freezing_golds - $data['money'],2,".","");
                        $userAmount->ava_pepper = number_format($userAmount->ava_pepper + $data['money'],2,".","");
                        break;
                    case 5://扣除解冻
                        $userAmount->freezing_golds = number_format($userAmount->freezing_golds - $data['money'],2,".","");
                        break;
                    default:
                        break;
                }
            }
            $userAmount->update_time = time();
            if ($userAmount->save()) return $userAmount;
        }
        return false;
    }

    public function addBusinessBank($data) {
        $userAmount = BusinessAccount::find()
            ->where(['bus_id' => $data['id']])
            ->one();
        if ($userAmount) {
            $userAmount->total_deposit = number_format($userAmount->total_deposit + $data['money'],2,".","");
            $userAmount->ava_deposit = number_format($userAmount->ava_deposit + $data['money'],2,".","");
            if ($userAmount->save()) {
                return $userAmount;
            }
        }
        return false;
    }
    //充值花椒
    public function addBusinessAccount($data)
    {
        return Yii::$app->db->createCommand()->insert('business_account', [
            'bus_id' => $data['bus_id'],
            'total_deposit' => isset($data['total_deposit']) ? $data['total_deposit'] : 0,
            'ava_deposit' => isset($data['ava_deposit']) ? $data['ava_deposit'] : 0,
            'create_time' => time()
        ])->execute();
    }
    //充值花椒
    public function addBusinessAccountPepper($data)
    {
        return Yii::$app->db->createCommand()->insert('business_account', [
            'bus_id' => $data['bus_id'],
            'ava_pepper' => isset($data['ava_pepper']) ? $data['ava_pepper'] : 0,
            'create_time' => time()
        ])->execute();
    }

    public function pushBank($data) {
        $businessAmount = BusinessAccount::find()
            ->where(['bus_id' => $data['push_role_id']])
            ->one();
        if ($businessAmount) {
            if (1 == $data['action_type']) {//押金
                $businessAmount->ava_deposit = number_format($businessAmount->ava_deposit + $data['deposit'],2,".","");
            }
            if (2 == $data['action_type']) {//花椒
                $businessAmount->ava_pepper = number_format($businessAmount->ava_pepper + $data['pepper'],2,".","");
            }
            if (3 == $data['action_type']) {//花椒押金
                $businessAmount->ava_deposit = number_format($businessAmount->ava_deposit + $data['deposit'],2,".","");
                $businessAmount->ava_pepper = number_format($businessAmount->ava_pepper + $data['pepper'],2,".","");
            }
            if ($businessAmount->save()) {
                return $businessAmount;
            }
        }
        return false;
    }

    public function pushBank_all($data) {
        $businessAmount = BusinessAccount::find()
            ->where(['bus_id' => $data['push_role_id']])
            ->one();
        if ($businessAmount) {
            if (1 == $data['action_type']) {//押金
                $businessAmount->ava_deposit = number_format($businessAmount->ava_deposit + $data['deposit'],2,".","");
                $businessAmount->total_deposit = number_format($businessAmount->total_deposit + $data['deposit'],2,".","");
            }
            if (2 == $data['action_type']) {//花椒
                $businessAmount->ava_pepper = number_format($businessAmount->ava_pepper + $data['pepper'],2,".","");
            }
            if (3 == $data['action_type']) {//花椒押金
                $businessAmount->ava_deposit = number_format($businessAmount->ava_deposit + $data['deposit'],2,".","");
	            $businessAmount->total_deposit = number_format($businessAmount->total_deposit + $data['deposit'],2,".","");
	            $businessAmount->ava_pepper = number_format($businessAmount->ava_pepper + $data['pepper'],2,".","");
            }
            if ($businessAmount->save()) {
                return $businessAmount;
            }
        }
        return false;
    }

    public function popBank($data) {
        $businessAmount = BusinessAccount::find()
            ->where(['bus_id' => $data['pop_role_id']])
            ->one();
	    if ($businessAmount) {
            if (1 == $data['action_type']) {//押金
                $businessAmount->ava_deposit = number_format($businessAmount->ava_deposit - $data['deposit'],2,".","");
                $businessAmount->total_deposit = number_format($businessAmount->total_deposit - $data['deposit'],2,".","");
            }
            if (2 == $data['action_type']) {//花椒
                $businessAmount->ava_pepper = number_format($businessAmount->ava_pepper - $data['pepper'],2,".","");
            }
            if (3 == $data['action_type']) {//花椒押金
                $businessAmount->ava_deposit = number_format($businessAmount->ava_deposit - $data['deposit'],2,".","");
	            $businessAmount->total_deposit = number_format($businessAmount->total_deposit - $data['deposit'],2,".","");
	            $businessAmount->ava_pepper = number_format($businessAmount->ava_pepper - $data['pepper'],2,".","");
            }
            if ($businessAmount->save()) {
                return $businessAmount;
            }
        }
        return false;
    }


	/**
	 * editAcount 修改商家资金 用于审核不通过
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/8
	 * @return int
	 * @throws \yii\db\Exception
	 */
	public function editAcount($data) {
		$sql = 'UPDATE ' . BusinessAccount::tableName() . ' SET freezing_deposit = :freezing_deposit,ava_deposit = :ava_deposit,ava_pepper = :ava_pepper,freezing_golds = :freezing_golds,update_time = :update_time WHERE bus_id = :bus_id ';
		return Yii::$app->db->createCommand($sql)
			->bindValues([
				':ava_deposit'      => $data['ava_deposit'],
				':ava_pepper'       => $data['ava_pepper'],
				':freezing_golds'   => $data['freezing_golds'],
				':freezing_deposit' => $data['freezing_deposit'],
				':update_time'      => time(),
				':bus_id'           => $data['bus_id']
			])->execute();
	}

    public function addBusAccount($data)
    {
        return Yii::$app->db->createCommand()->insert('business_account', [
            'bus_id' =>  $data['bus_id'],
            'total_deposit' => $data['total_deposit'],
            'ava_deposit' => $data['ava_deposit'],
            'freezing_deposit' => $data['freezing_deposit'],
            'ava_pepper' => $data['ava_pepper'],
            'freezing_golds' => $data['freezing_golds'],
            "cash_deposit" => $data['cash_deposit'],
            'create_time' => time()
        ])->execute();
    }
}